package com.example.wordcardjavaproject.error;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Luna
 */
@ControllerAdvice()
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Map<String,Object> errorResult(RuntimeException e){
        // 注意：在实际的公司开发过程中，要把错误记录在日志中
        log.error(e.getMessage());
        Map<String,Object> errorResultMap=new HashMap<String,Object>();
        errorResultMap.put("code",500);
        errorResultMap.put("msg",e.getMessage());
        return errorResultMap;
    }
}
